package com.sjz.han.industry.activites;

import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sjz.han.industry.MainActivity;
import com.sjz.han.industry.R;
import com.sjz.han.industry.base.BaseActivity;
import com.sjz.han.industry.entities.LoignEntity;
import com.sjz.han.industry.internetmanage.ReqestUrl;
import com.sjz.han.industry.utils.JsonUtils;
import com.sjz.han.industry.utils.SysUtils;
import com.sjz.han.industry.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

//登陆界面
public class LoginActivity extends BaseActivity implements View.OnClickListener{
    private TextView login_regist_tv,login_alter_tv;
    private boolean isYzm=false;//false 验证码，true 账号密码

    private LinearLayout login_YZM_ll;
    private EditText login_pwd_edt;

    private Button login_btn;
    private EditText login_phone_edt,login_code_edt;
    private TextView login_getcode_tv;
    private String phone="",code="",pwd="";

    @Override
    protected int setLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initDatas() {
//        15831929901   123  c2 5Y3UKR CJ7GPB  ULD0K4
    }

    @Override
    protected void initViews() {
        login_regist_tv=(TextView)this.findViewById(R.id.login_regist_tv);
        login_alter_tv=(TextView)this.findViewById(R.id.login_alter_tv);
        login_btn=(Button)this.findViewById(R.id.login_btn);

        login_YZM_ll=(LinearLayout)this.findViewById(R.id.login_YZM_ll);
        login_pwd_edt=(EditText)this.findViewById(R.id.login_pwd_edt);
        login_pwd_edt.setVisibility(View.GONE);

        login_phone_edt=(EditText)this.findViewById(R.id.login_phone_edt);
        login_code_edt=(EditText)this.findViewById(R.id.login_code_edt);

        login_getcode_tv=(TextView)this.findViewById(R.id.login_getcode_tv);
    }

    @Override
    protected void setLisener() {
        login_regist_tv.setOnClickListener(this);
        login_btn.setOnClickListener(this);
        login_alter_tv.setOnClickListener(this);
        login_getcode_tv.setOnClickListener(this);
    }

    @Override
    protected void onRetry() {

    }

    @Override
    public void onClick(View v) {

        phone=login_phone_edt.getText().toString().trim();
        code=login_code_edt.getText().toString().trim();
        pwd=login_pwd_edt.getText().toString().trim();

        switch (v.getId()){
            case R.id.login_btn:
                if(isYzm){//账号密码登录
                    if(SysUtils.isNetworkAvailable(this)){
                        if(TextUtils.isEmpty(phone)){
                            ToastUtils.popUpToast("手机号不可为空！");
                        }else if(TextUtils.isEmpty(pwd)){
                            ToastUtils.popUpToast("密码不可为空！");
                        }else{
                           loginByPwd();
                        }
                    }else{
                        ToastUtils.popUpToast(getResources().getString(R.string.text_nonet));
                    }
                }else{//验证码登陆
                    if(SysUtils.isNetworkAvailable(this)){
                        if(TextUtils.isEmpty(phone)){
                            ToastUtils.popUpToast("手机号不可为空！");
                        }else if(TextUtils.isEmpty(code)){
                            ToastUtils.popUpToast("验证码不可为空！");
                        }else{
                            loginByYzm();
                        }
                    }else{
                        ToastUtils.popUpToast(getResources().getString(R.string.text_nonet));
                    }
                }
                break;
            case R.id.login_getcode_tv://获取验证码
                if(SysUtils.isNetworkAvailable(this)){
                    if(TextUtils.isEmpty(phone)){
                        ToastUtils.popUpToast("手机号不可为空！");
                    }else{
                        getCheckCode(phone);
                    }
                }else{
                    ToastUtils.popUpToast(getResources().getString(R.string.text_nonet));
                }
                break;
            case R.id.login_regist_tv:
                startActivity(new Intent(this, RegistActivity.class));
                break;
            case R.id.login_alter_tv:
                if(isYzm){
                    isYzm=false;
                    login_YZM_ll.setVisibility(View.VISIBLE);
                    login_pwd_edt.setVisibility(View.GONE);
                    login_alter_tv.setText("使用密码登录");
                }else{
                    isYzm=true;//变成账号密码登录
                    login_YZM_ll.setVisibility(View.GONE);
                    login_pwd_edt.setVisibility(View.VISIBLE);
                    login_alter_tv.setText("使用验证码登录");
                }
                break;
        }
    }

    /**
     * ==========================获取验证码==============================
     * @param phone
     */
    private TimeCount timeCount=null;
    public void getCheckCode(String phone){
        timeCount = new TimeCount(60000,1000);
        timeCount.start();
        OkHttpUtils
                .post()
                .url(ReqestUrl.CHECKCODE_URL)
                .addParams("phone",phone)
                .addParams("type", "login")
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtils.popUpToast("请求超时");
            }
            @Override
            public void onResponse(String response, int id) {
                if(ReqestUrl.rebacRequestJson(response)!=null){
                    LoignEntity entity= JsonUtils.getObject(response,LoignEntity.class);
                    if(entity!=null&&entity.code==0){
                        ToastUtils.popUpToast(entity.msg);
                    }else{
                        ToastUtils.popUpToast("接口请求失败");
                    }
                }
            }
        });
    }
    //计时器
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInfuture,long countDownInterval){
            super(millisInfuture,countDownInterval);
        }
        @Override
        public void onTick(long millisUntilFinished) {
            login_getcode_tv.setText(millisUntilFinished/1000+"秒");
            login_getcode_tv.setEnabled(false);
        }

        @Override
        public void onFinish() {
            login_getcode_tv.setEnabled(true);
            login_getcode_tv.setText("获取验证码");
        }
    }
    /**
     * ==========================获取验证码==============================
     */

    public void loginByYzm(){
        showLoadingDialog("正在登陆...");
        OkHttpUtils
                .post()
                .url(ReqestUrl.LOGIN_CODE_URL)
                .addParams("phone",phone)
                .addParams("code",code)
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.popUpToast("请求超时");
            }
            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if(ReqestUrl.rebacRequestJson(response)!=null){
                    LoignEntity entity= JsonUtils.getObject(response,LoignEntity.class);
                    if(entity!=null&&entity.code==0){
                        ToastUtils.popUpToast(entity.msg);
                        shareUtils.setIsFirstRunning(true);
                        shareUtils.setToken(entity.data.token);
                        shareUtils.setLevel(entity.data.level);
                        shareUtils.setYQM(entity.data.inviteCode);
                        if("0".equals(entity.data.level)){//默认0， 线上支付后为1也就是普通会员
                            startActivity(new Intent(LoginActivity.this, PayActivity.class));
                        }else if("1".equals(entity.data.level)){
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        }
                        finish();

                    }else{
                        ToastUtils.popUpToast(entity.msg);
                    }
                }
            }
        });
    }
    public void loginByPwd(){
        showLoadingDialog("正在登陆...");
        OkHttpUtils
                .post()
                .url(ReqestUrl.LOGIN_PWD_URL)
                .addParams("phone",phone)
                .addParams("pwd",pwd)
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.popUpToast("请求超时");
            }
            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if(ReqestUrl.rebacRequestJson(response)!=null){
                    LoignEntity entity= JsonUtils.getObject(response,LoignEntity.class);
                    if(entity!=null&&entity.code==0){
                        ToastUtils.popUpToast(entity.msg);
                        shareUtils.setIsFirstRunning(true);
                        shareUtils.setToken(entity.data.token);
                        shareUtils.setLevel(entity.data.level);
                        shareUtils.setYQM(entity.data.inviteCode);
                        if("0".equals(entity.data.level)){//默认0， 线上支付后为1也就是普通会员
                            startActivity(new Intent(LoginActivity.this, PayActivity.class));
                        }else{
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        }
                        finish();

                    }else{
                        ToastUtils.popUpToast(entity.msg);
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }
}
